<?php defined('InNcPHP') or exit('Access Invalid!');?>
<script src="//webapi.amap.com/maps?v=1.3&key=9278aff6ea76c052364dd8216fc9be47&plugin=AMap.Geocoder"></script>
<style>
    .show { display: block; }
    .hide { display: none; }
</style>
<div class="page">
    <div class="fixed-bar">
        <div class="item-title">
            <h3>旅行社</h3>
            <?php echo $output['top_link'];?>
        </div>
    </div>
    <div class="fixed-empty"></div>
    <!-- 提交表单开始 -->
    <form id="ticket_form" method="post" enctype="multipart/form-data">
        <input type="hidden" name="form_submit" value="ok" />
        <table class="table tb-type2 nobdb">
            <tbody>
            <!-- 所属商家 -->
            <tr>
                <td colspan="2" class="required">
                    <label class="validation"><?php echo $lang['ticket_index_merchant'];?>: </label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform">
                    <select name="merchant_id">
                        <option value=""><?php echo $lang['nc_please_choose'];?>...</option>
                        <?php if (is_array($output['merchants'])): ?>
                            <?php foreach ($output['merchants'] as $k => $v): ?>
                                <option value="<?php echo $v['merchant_id']; ?>"><?php echo $v['mc_shop_name']; ?></option>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                </td>
                <td class="vatop tips"></td>
            </tr>
            <!-- 名称 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation">线路名称:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" name="title" class="txt"></td>
                <td class="vatop tips"><?php echo $lang['ticket_add_title_notice'];?></td>
            </tr>
            <!-- 分类 -->
            <tr>
                <td colspan="2" class="required">
                    <label class="validation"><?php echo $lang['ticket_index_class'];?>: </label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform">
                    <select id="cate1" name="cate1" class="class-select">
                        <option value="">选择分类</option>
                        <?php if (is_array($output['cate'])): ?>
                            <?php foreach ($output['cate'] as $k => $v): ?>
                                <option value="<?php echo $v['tc_id']; ?>" <?php echo $output['hotel_info']['cate_id'] == $v['tc_id'] ? 'selected' : ''; ?>><?php echo $v['tc_name']; ?></option>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                </td>
                <td class="vatop tips"></td>
            </tr>
            <tr>
                <td colspan="2" class="required"><label class="validation" for="hotel_tel">电话:</label></td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" name="hotel_tel" id="hotel_tel" class="txt"></td>
                <td class="vatop tips"></td>
            </tr>

          <!-- 默认显示价格 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation">默认展示价格:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" name="show_price" class="txt"></td>
                <td class="vatop tips"></td>
            </tr>
            <!-- 默认显示价格 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation">售价:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" name="price" class="txt"></td>
                <td class="vatop tips"></td>
            </tr>
            <!-- 成本价 -->
            <!--<tr class="noborder close_spec">
                <td colspan="2" class="required">
                    <label class="validation">协议价:</label>
                </td>
            </tr>
            <tr class="noborder close_spec" >
                <td class="vatop rowform"><input type="text" name="cost_price" class="txt"></td>
                <td class="vatop tips"></td>
            </tr>-->
            <!-- 库存 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation">库存:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" name="stock" class="txt"></td>
                <td class="vatop tips"></td>
            </tr>
            <!-- 销量 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation">销量:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" name="sales" class="txt"></td>
                <td class="vatop tips"></td>
            </tr>
            <!-- 标签 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation">标签:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" name="label" class="txt"></td>
                <td class="vatop tips"></td>
            </tr>

            <tr class="add_open_spec">
                <td colspan="2" class="required"><label class="validation">是否可购买:</label> </td>
            </tr>
            <tr class="noborder add_open_spec">
                <td class="vatop rowform onoff">
                    <label for="is_purchase1" class="cb-enable selected "><span><?php echo $lang['nc_yes'];?></span></label>
                    <label for="is_purchase0" class="cb-disable "><span><?php echo $lang['nc_no'];?></span></label>
                    <input id="is_purchase1" name="is_purchase"  checked value="1" type="radio">
                    <input id="is_purchase0" name="is_purchase" value="0" type="radio"></td>
                <td class="vatop tips"></td>
            </tr>
            <!-- 地址 -->
            <tr>
                <td colspan="2" class="required">
                    <label class="validation"><?php echo $lang['ticket_add_area'];?>: </label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform">
                    <select id="store_areaId_01" name="province_id" class="class-select">
                        <option value="">省份</option>
                        <?php if (is_array($output['provinces'])): ?>
                            <?php foreach ($output['provinces'] as $k => $v): ?>
                                <option value="<?php echo $v['area_id']; ?>"><?php echo $v['name']; ?></option>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                    <select id="store_areaId_02" name="city_id" class="class-select">
                        <option value="">城市</option>
                    </select>
                    <select id="store_areaId_03" name="area_id" class="class-select">
                        <option value="">区/县</option>
                    </select>
                </td>
                <td class="vatop tips"></td>
            </tr>
            <!-- 详细地址 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation"><?php echo $lang['ticket_add_addr'];?>:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" name="addr" id="addr" class="txt"></td>
                <td class="vatop tips"><?php echo $lang['ticket_add_addr_notice'];?></td>
            </tr>
            <tr>
                <td colspan="2" class="required"><label class="validation" for="store_location">经纬度lng&lat:</label></td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" value="" name="store_location" readonly="true" id="store_location" class="txt"></td>
                <td class="vatop tips"></td>
            </tr>

            <tr class="noborder">
                <td class="vatop rowform"><button type="button" class="btn" id="getMapLocation">地图获取坐标</button></td>
                <td class="vatop tips"></td>
            </tr>
            <!-- 封面图片 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation"><?php echo $lang['ticket_add_image'];?>:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><span class="type-file-show">
                    </span> <span class="type-file-box">
                        <input type='text' name='textfield' id='textfield' class='type-file-text' />
                        <input type='button' name='button' id='button' value='' class='type-file-button' />
                        <input name="thumb" type="file" class="type-file-file" id="image" size="30" hidefocus="true" />
                    </span>
                </td>
                <td class="vatop tips"><?php echo $lang['ticket_add_image_notice'];?></td>
            </tr>

            <!-- 酒店介绍 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation">行程安排:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td colspan="2" class="vatop rowform">
                    <textarea id="content" name="content" style="width:70%;height: 400px;"></textarea>
                </td>
            </tr>
            <!-- 酒店介绍 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation">购买须知:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td colspan="2" class="vatop rowform">
                    <textarea id="content1" name="content1" style="width:70%;height: 400px;"></textarea>
                </td>
            </tr>
            </tbody>


            <tfoot>
            <!-- 提交 -->
            <tr class="tfoot">
                <td colspan="2" >
                    <a href="JavaScript:void(0);" class="btn" id="submitBtn">
                        <span><?php echo $lang['nc_submit'];?></span>
                    </a>
                </td>
            </tr>
            </tfoot>
        </table>
    </form>
</div>

<script type="text/javascript" SRC="./resource/kindeditor/kindeditor-min.js"></script>
<script>
    //门票类型改变选中状态（儿童票、成人票等）
    function changeUse(obj) {
        var td = obj.parent().parent().parent().next();
        if (obj.is(':checked') == false) {
            td.hide().find('input').val('');
        } else {
            td.show();
        }
    }
    //价格类型改变事件（固定价格、每日价格）
    $('input[name="type"]').change(function () {
        if (this.value == 0) {
            $('.type_fix:not(.inputs)').show();
        } else {
            $('input[name^="types"]:checked').each(function (i) {
                changeUse($(this).prop('checked',false));
            });
            $('.type_fix').hide();
        }
    });

    //实例化富文本编辑器
    KindEditor.ready(function(K){
        K.create('textarea[name="content"]', {
            themeType: 'simple',
            resizeType: 1,
            uploadJson: './resource/kindeditor/php/upload_json.php',
            fileManagerJson: './resource/kindeditor/php/file_manager_json.php',
            allowFileManager: true,
            afterBlur: function(){this.sync();}
        });
        K.create('textarea[name="content1"]', {
            themeType: 'simple',
            resizeType: 1,
            uploadJson: './resource/kindeditor/php/upload_json.php',
            fileManagerJson: './resource/kindeditor/php/file_manager_json.php',
            allowFileManager: true,
            afterBlur: function(){this.sync();}
        });
    });
    //选择省份
    $("#store_areaId_01").change(function(){

        var _areaID = $(this).val();
        _this = $("#store_areaId_01");
        if(_areaID){
            $.getJSON("./index.php?act=common&op=getAreaChild&parent_id=" + _areaID, function(data){
                $obj = $("#store_areaId_02");
                $obj.empty();
                $obj.append('<option value="">城市</option>');
                $obj.nextAll("select").empty();
                $obj.nextAll("select").append('<option value="">区/县</option>');
                $.each(data, function(i,item){
                    $obj.append('<option value="'+item.area_id+'">'+item.name+'</option>');
                });
            });
        }else{
            _this.nextAll("select").empty();
            _this.next("select").append('<option value="">城市</option>');
            _this.next("select").next('select').append('<option value="">区/县</option>');
        }
    });
    //选择城市
    $("#store_areaId_02").change(function(){
        var _areaID = $(this).val();
        _this = $("#store_areaId_2");
        if(_areaID){
            $.getJSON("index.php?act=common&op=getAreaChild&parent_id=" + _areaID, function(data){
                $obj = $("#store_areaId_03");
                $obj.empty();
                $obj.append('<option value="">区/县</option>');
                $.each(data, function(i,item){
                    $obj.append('<option value="'+item.area_id+'">'+item.name+'</option>');
                });
            });
        }else{
            _this.nextAll("select").empty();
            _this.next("select").append('<option value="">区/县</option>');
        }
    });
    //选择区/县
    $("#store_areaId_03").change(function(){
        var _areaID = $(this).val();
        if(_areaID){
            var area_p = $("#store_areaId_01").find("option:selected").text();
            var area_c = $("#store_areaId_02").find("option:selected").text();
            var area_a = $("#store_areaId_03").find("option:selected").text();
        }
    });
    //选择一级分类
    /*    $("#cate1").change(function(){
     var _tcId = $(this).val();
     _this = $(this);
     if(_tcId){
     $.getJSON("index.php?act=common&op=getCategory&tc_pid=" + _tcId, function(data){
     $obj = $("#cate2");
     $obj.empty();
     $obj.append('<option value="">选择二级分类</option>');
     $.each(data, function(i,item){
     $obj.append('<option value="'+item.tc_id+'">'+item.tc_name+'</option>');
     });
     });
     }else{
     _this.next("select").empty();
     _this.next("select").append('<option value="">选择二级分类</option>');
     }
     });*/
    //图片文件选择
    $("#image").change(function(){
        $("#textfield").val($(this).val());
    });
    //图片文件选择
    $("#image1").change(function(){
        $("#textfield1").val($(this).val());
    });

    //提交表单
    $("#submitBtn").click(function(){
        if ($("#ticket_form").valid()) {
            if (parseInt($('input[name="type"]:checked').val()) === 0 && $('input[name^="types"]:checked').length === 0) {
                layer.alert('<?php echo $lang['ticket_add_use_null']; ?>', function (index) {
                    $('html,body').animate({scrollTop: $('input[name^="types"]:eq(0)').offset().top - 100}, 300);
                    layer.close(index);
                });
            } else {
                //alert('debug:表单校验ok');
                $("#ticket_form").submit();
            }
        } else {
            $('html,body').animate({scrollTop: $('label.error:visible:eq(0)').offset().top - 70}, 300);
        }
    });

    //获取经纬度
    $(document).on("click","#getMapLocation",function(){
        var proviceId= $("#store_areaId_01").val();
        var cityId = $("#store_areaId_02").val();
        var areaId = $("#store_areaId_03").val();
        if(cityId == ''){
            alert('请先选择所在地区');
            return false;
        }
        /*    if($("#store_areaId_3 option").length!=1&&areaId == ''){
         alert('请先选择所在区县');
         return false;
         }*/
        $.ajax({
            type: "GET",
            url: "index.php",
            data: {act:'common',op:'bdmap',proviceId:proviceId,cityId:cityId,areaId:areaId,store_address:$("#addr").val()},
            dataType: "html",
            success: function(data){
                $('body').append(data);
            }
        });
    });
    //表单校验
    $("#ticket_form").validate({
        errorPlacement: function(error, element){
            if (element.attr('type') === 'file') {
                error.appendTo(element.parent().parent().parent().prev().find('td:first'));
            } else {
                error.appendTo(element.parent().parent().prev().find('td:first'));
            }
        },
        rules: {
            merchant_id: 'required',
            title: {
                required: true,
                minlength: 3,
                maxlength: 255
            },
            subtitle: {
                required: true,
                minlength: 3,
                maxlength: 255
            },
            cate1: 'required',
            /*cate2: 'required',*/

            show_price: {
                required: true,
                number: true,
                min: 0.01,
                max: 9999999
            },
            price: {
                required: true,
                number: true,
                min: 0.01,
                max: 9999999
            },
            province_id: 'required',
            city_id: 'required',
            area_id: 'required',
            addr: 'required',
            thumb: 'required',
            face: 'required',
            content: 'required',
            content1: 'required',
            store_location:'required',
            hotel_tel:'required',
            stock:'required',
            label:"required",
            cost_price:'required',
        },
        messages : {
            merchant_id: '<?php echo $lang['ticket_add_merchant_null']; ?>',
            title: '<?php echo $lang['ticket_add_title_notice']; ?>',
            subtitle: '<?php echo $lang['ticket_add_title_notice']; ?>',
            cate1: '请选择分类',
            cate2: '<?php echo $lang['ticket_add_cate2_null']; ?>',

            show_price: '<?php echo $lang['ticket_add_price_notice']; ?>',
            price: '<?php echo $lang['ticket_add_price_notice']; ?>',
            province_id: '<?php echo $lang['ticket_add_province_null']; ?>',
            city_id: '<?php echo $lang['ticket_add_city_null']; ?>',
            area_id: '<?php echo $lang['ticket_add_area_null']; ?>',
            addr: '<?php echo $lang['ticket_add_addr_null']; ?>',
            thumb: '<?php echo $lang['ticket_add_image_null']; ?>',
            face: '<?php echo $lang['ticket_add_image_null']; ?>',
            content: '请填写行程安排',
            content1: '购买须知不能为空',
            store_location:'地理坐标不能为空',
            hotel_tel:'酒店电话不能为空',
            stock:'库存不能为空',
            label:'标签不能为空',
            cost_price:'成本价不能为空',
        }
    });
</script>
